package net.siufung.mybatis.permit;

import com.google.common.base.Strings;
import lombok.Data;
import net.siufung.core.model.enums.DataScopeEnum;
import net.siufung.core.utils.ValidationUtil;
import net.siufung.core.utils.ValidationUtil.ErrorMessage;
import net.siufung.core.utils.ValidationUtil.ValidResult;
import net.siufung.core.utils.ValidationUtil.Validatable;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @author 陈建峰
 * @since 2022/7/25 6:25 下午
 */
@Data
public class DataPermitModel implements Validatable {

    private static final long serialVersionUID = 1089934882570743936L;

    /**
     * 获取数据权限方法
     */
    @NotBlank(message = "数据权限请求方法不能为空！")
    private String method;

    /**
     * 数据权限对应字段
     */
    private String column;

    /**
     * 获取数据权限类型
     */
    @NotNull(message = "数据权限类型不能为空！")
    private DataScopeEnum dataScopeType;

    /**
     * 数据权限值
     */
    private String dataScopeValue;

    @Override
    public ValidResult validResult() {
        ValidResult validResult = ValidationUtil.validateBean(this);
        if(!DataScopeEnum.ALL.equals(dataScopeType)){
            if(Strings.isNullOrEmpty(dataScopeValue)){
                validResult.addError(new ErrorMessage("dataScopeValue", "数据权限值不能为空！"));
            }
        }
        return validResult;
    }
}
